package com.lgt_enterprise_android.view.pages;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.lgt_enterprise_android.R;

public class EmptyActivity extends AppCompatActivity {

    private ImageView iv_menu1;
    private ImageView iv_menu2;
    private ImageView iv_menu3;
    private ImageView iv_add;
    //启始坐标 x，y
    private float startX;
    private float startY;

    private boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty);

        iv_add = findViewById(R.id.iv_add);
        iv_menu1 = findViewById(R.id.iv_men1);
        iv_menu2 = findViewById(R.id.iv_men2);

        iv_menu3 = findViewById(R.id.iv_men3);


    }


    public void startAnim(View view) {
        startX = iv_add.getTranslationX();
        startY = iv_add.getTranslationY();

        //x轴偏移量
        float endX_menu1 = getScreenWidth() / 3;
        //Y轴坐标 向上偏移量300px  可自己根据dp转换成px
        float endY_menu1 = startY - 200;

        Log.d("Tag", "startX=" + startX + " startY =" + startY);
        Log.d("Tag", "endX_menu1=" + endX_menu1 + " endY_menu1=" + endY_menu1);

        if (flag) {
            //点击按钮的旋转动画
            ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", 0, 270);
            rotation.setDuration(500);
            rotation.start();

            //分别设置x轴Y轴的偏移量
            ObjectAnimator menu1_animatorX = ObjectAnimator.ofFloat(iv_menu1, "translationX", startX, endX_menu1);
            ObjectAnimator menu1_animatorY = ObjectAnimator.ofFloat(iv_menu1, "translationY", startY, endY_menu1);
            //缩放动画
            ObjectAnimator menu1_animator_scaleX = ObjectAnimator.ofFloat(iv_menu1, "scaleX", 0f, 1f);
            ObjectAnimator menu1_animator_scaleY = ObjectAnimator.ofFloat(iv_menu1, "scaleY", 0f, 1f);

            AnimatorSet menu1Set = new AnimatorSet();
            menu1Set.setDuration(500);
            //OvershootInterpolator插值器实现动画抖动效果
            menu1Set.setInterpolator(new OvershootInterpolator());
            menu1Set.playTogether(menu1_animatorX, menu1_animatorY, menu1_animator_scaleX, menu1_animator_scaleY);
            menu1Set.start();


            ObjectAnimator menu2_animatorX = ObjectAnimator.ofFloat(iv_menu2, "translationX", startX, -endX_menu1);
            ObjectAnimator menu2_animatorY = ObjectAnimator.ofFloat(iv_menu2, "translationY", startY, endY_menu1);
            ObjectAnimator menu2_animator_scaleX = ObjectAnimator.ofFloat(iv_menu2, "scaleX", 0f, 1f);
            ObjectAnimator menu2_animator_scaleY = ObjectAnimator.ofFloat(iv_menu2, "scaleY", 0f, 1f);
            AnimatorSet menu2Set = new AnimatorSet();
            menu2Set.setDuration(500);
            menu2Set.setInterpolator(new OvershootInterpolator());
            menu2Set.playTogether(menu2_animatorX, menu2_animatorY, menu2_animator_scaleX, menu2_animator_scaleY);
            menu2Set.start();


            ObjectAnimator menu3_animatorX = ObjectAnimator.ofFloat(iv_menu3, "translationX", startX, -startX);
            ObjectAnimator menu3_animatorY = ObjectAnimator.ofFloat(iv_menu3, "translationY", startY, endY_menu1);
            ObjectAnimator menu3_animator_scaleX = ObjectAnimator.ofFloat(iv_menu3, "scaleX", 0f, 1f);
            ObjectAnimator menu3_animator_scaleY = ObjectAnimator.ofFloat(iv_menu3, "scaleY", 0f, 1f);
            AnimatorSet menu3Set = new AnimatorSet();
            menu3Set.setDuration(500);
            menu3Set.setInterpolator(new OvershootInterpolator());
            menu3Set.playTogether(menu3_animatorX, menu3_animatorY, menu3_animator_scaleX, menu3_animator_scaleY);
            menu3Set.start();


        } else {

            ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", 270, 0);
            rotation.setDuration(500);
            rotation.start();

            ObjectAnimator menu1_animatorX = ObjectAnimator.ofFloat(iv_menu1, "translationX", endX_menu1, startX);
            ObjectAnimator menu1_animatorY = ObjectAnimator.ofFloat(iv_menu1, "translationY", endY_menu1, startY);

            ObjectAnimator menu1_animator_scaleX = ObjectAnimator.ofFloat(iv_menu1, "scaleX", 1f, 0f);
            ObjectAnimator menu1_animator_scaleY = ObjectAnimator.ofFloat(iv_menu1, "scaleY", 1f, 0f);

            AnimatorSet menu1Set = new AnimatorSet();
            menu1Set.setDuration(500);
            menu1Set.playTogether(menu1_animatorX, menu1_animatorY, menu1_animator_scaleX, menu1_animator_scaleY);
            menu1Set.start();


            ObjectAnimator menu2_animatorX = ObjectAnimator.ofFloat(iv_menu2, "translationX", -endX_menu1, startX);
            ObjectAnimator menu2_animatorY = ObjectAnimator.ofFloat(iv_menu2, "translationY", endY_menu1, startY);
            ObjectAnimator menu2_animator_scaleX = ObjectAnimator.ofFloat(iv_menu2, "scaleX", 1f, 0f);
            ObjectAnimator menu2_animator_scaleY = ObjectAnimator.ofFloat(iv_menu2, "scaleY", 1f, 0f);
            AnimatorSet menu2Set = new AnimatorSet();
            menu2Set.setDuration(500);
            menu2Set.playTogether(menu2_animatorX, menu2_animatorY, menu2_animator_scaleX, menu2_animator_scaleY);
            menu2Set.start();


        }

        flag = !flag;

    }


    public void menu1Click(View v) {
        Toast.makeText(getApplicationContext(), "menu1 click", Toast.LENGTH_SHORT).show();
    }

    public void menu2Click(View v) {
        Toast.makeText(getApplicationContext(), "menu2 click", Toast.LENGTH_SHORT).show();
    }


    private int getScreenWidth() {
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        return width;
    }


    private int getScreenHeight() {
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int height = dm.heightPixels;
        return height;
    }


}